Imports System.Threading.Thread
Imports Q7P1TTS.Lib

Public Class MainForm
    Dim t As New System.Timers.Timer(1000)
    Public isCustomer As Boolean = False
    Public isEmployee As Boolean = False
    Public isCREmployeeStaff As Boolean = False
    Public isCREmployeeLead As Boolean = False
    Public isOPEmployeeStaff As Boolean = False
    Public isOPEmployeeLead As Boolean = False
    Public isAdmin As Boolean = False
    Public loginForm As New LoginForm()
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler t.Elapsed, AddressOf t_Elapsed
        t.Start()
        ReloadMenu()
        GoToForm(New Welcome(), "Welcome")
    End Sub

    Private Sub t_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        t.Stop()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf OpenLogin))
        Else
            loginForm.ShowDialog(Me)
        End If

    End Sub

    Private Sub OpenLogin()
        loginForm.ShowDialog(Me)
    End Sub

    Public Sub ReloadMenu()
        Dim cust As New Customer()
        Dim emp As New Employee()
        If TypeOf SessionHelper.Sessions("USER_INFO") Is Customer Then
            cust = SessionHelper.Sessions("USER_INFO")
        ElseIf TypeOf SessionHelper.Sessions("USER_INFO") Is Employee Then
            emp = SessionHelper.Sessions("USER_INFO")
        End If
        isCustomer = cust.ID > 0
        isEmployee = emp.ID > 0
        If isEmployee Then
            isCREmployeeStaff = isEmployee And emp.Department.Name = "Customer Relation" And emp.Designation = "Staff"
            isCREmployeeLead = isEmployee And emp.Department.Name = "Customer Relation" And emp.Designation = "Lead"
            isOPEmployeeStaff = isEmployee And emp.Department.Name = "Operational" And emp.Designation = "Staff"
            isOPEmployeeLead = isEmployee And emp.Department.Name = "Operational" And emp.Designation = "Lead"
        End If
       
        isAdmin = Convert.ToBoolean(SessionHelper.Sessions("IS_ADMIN"))

        pageAccount.Visible = True
        btnAccountLogin.Visible = (Not isCustomer) And (Not isEmployee) And (Not isAdmin)
        btnAccountLogout.Visible = isCustomer Or isEmployee Or isAdmin
        btnAccountRegister.Visible = (Not isCustomer) And (Not isEmployee) And (Not isAdmin)
        btnAccountChangePwd.Visible = isCustomer Or isEmployee

        pageCustomer.Visible = isAdmin
        pageEmployee.Visible = isAdmin

        pageComplaint.Visible = isCustomer Or isEmployee Or isAdmin
        btnComplaintMy.Visible = isOPEmployeeStaff Or isOPEmployeeLead
        btnComplaintPendingList.Visible = isOPEmployeeLead Or isAdmin
        btnComplaintSubmit.Visible = isCustomer
        btnComplaintList.Visible = isCREmployeeStaff Or isCREmployeeLead Or isAdmin
        btnComplaintAssign.Visible = isOPEmployeeLead Or isAdmin
        btnComplaintStatus.Visible = isCustomer

        pageNewService.Visible = isCustomer Or isEmployee Or isAdmin
        btnNewServiceAdd.Visible = isAdmin

    End Sub

    Public Sub GoToForm(ByVal Form As Control)
        For Each ctrl As Control In grpMainForm.Panel.Controls
            ctrl.Dispose()
        Next
        grpMainForm.Panel.Controls.Clear()
        grpMainForm.Panel.Controls.Add(Form)
    End Sub

    Public Sub GoToForm(ByVal Form As Control, ByVal Header As String)
        Me.GoToForm(Form)
        Me.grpMainForm.ValuesPrimary.Heading = Header
    End Sub
    Public Sub SetHeader(ByVal Header As String)
        Me.grpMainForm.ValuesPrimary.Heading = Header
    End Sub
    Private Sub btnEmployeeAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployeeAdd.Click
        GoToForm(New EmployeeForm(), "Add New Employee")
    End Sub

    Private Sub btnCustomerAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomerAdd.Click
        GoToForm(New CustomerForm(), "Add New Customer")
    End Sub

    Private Sub btnEmployeeList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployeeList.Click
        GoToForm(New EmployeeList(), "Employee List")
    End Sub

    Private Sub btnComplaintSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComplaintSubmit.Click
        GoToForm(New FeedBackForm(), "Submit Feedback")
    End Sub

    Private Sub btnAccountLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccountLogin.Click
        Dim loginForm As New LoginForm()
        loginForm.ShowDialog(Me)
    End Sub

    Private Sub btnAccountLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccountLogout.Click
        SessionHelper.Sessions("USER_INFO") = Nothing
        SessionHelper.Sessions("IS_ADMIN") = False
        GoToForm(New Welcome(), "Welcome")
        ReloadMenu()
    End Sub

    Private Sub btnAccountRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccountRegister.Click
        GoToForm(New CustomerForm(), "Registration")
    End Sub

    Private Sub btnCustomerList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomerList.Click
        GoToForm(New CustomerList(), "Customer List")
    End Sub

    Private Sub btnComplaintMy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComplaintMy.Click
        GoToForm(New FeedbackList(1), "My Assigned Complaint")
    End Sub

    Private Sub btnComplaintList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComplaintList.Click
        GoToForm(New FeedbackList(0), "Complaint List")
    End Sub

    Private Sub btnAccountChangePwd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccountChangePwd.Click
        Dim cp As New ChangePasswordForm()
        cp.ShowDialog(Me)
    End Sub

    Private Sub btnComplaintPendingList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComplaintPendingList.Click
        GoToForm(New FeedbackList(2), "Pending Complaint")
    End Sub

    Private Sub btnComplaintAssign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComplaintAssign.Click
        GoToForm(New AssignForm(), "Assign Complaint")
    End Sub

    Private Sub btnNewServiceAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewServiceAdd.Click
        GoToForm(New ServiceForm(), "New Service")
    End Sub

    Private Sub btnNewServiceList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewServiceList.Click
        GoToForm(New ServiceList(), "New Service List")
    End Sub

    Private Sub btnComplaintStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComplaintStatus.Click
        GoToForm(New FeedbackList(3), "My Complaint Status")
    End Sub
End Class
